<?php

namespace app\xwam\controller;

/**
 * File 控制器类
 *
 * @version 1.0 2015-10-24 SoChishun(14507247@qq.com) Added.
 */
class FilesController extends AppbaseController {

    function indexSearch() {
        $search = input('param.');
        $where = array();
        if (!empty($search['word'])) {
            $word = $search['word'];
            $where['_string'] = "title like '%$word%' or file_name like '%$word%'";
        }
        if (!empty($search['category_id'])) {
            $where['category_id'] = $search['category_id'];
        }
        if (!empty($search['title'])) {
            $where['title'] = array('like', '%' . $search['title'] . '%');
        }
        //$where['site_id']=$this->site_id;
        return array('search' => $search, 'where' => $where);
    }

    function index() {
        $asearch = $this->indexSearch();
        $m_file = new \app\xwam\model\FilesModel();
        $list = $m_file->selectPaging($page, array('where' => $asearch['where'], 'order' => 'sort, id desc'), array('page_params' => $asearch['search']));
        $this->assign('list', $list);
        $this->assign('page', $list->render());
        $this->assign('search', $asearch['search']);
        return view();
    }

    function edit($id = 0, $category_id = 0) {
        $data = array();
        if ($id) {
            $m_file = new \app\xwam\model\FilesModel();
            $data = $m_file->find($id);
        }
        if (!$data) {
            $data = array('user_name' => $this->userName, 'site_id' => $this->siteId);
        }
        if ($category_id) {
            $data['category_id'] = $category_id;
        }
        $this->assign('data', $data);
        return view();
    }

    function editSave() {
        $m_file = new \app\xwam\model\FilesModel();
        $result = $m_file->saveFile();
        $this->dialogJump($result['status'], $result['info']);
    }

    function changeSort($id, $sort) {
        $m_file = new \app\xwam\model\FilesModel();
        return json_encode($m_file->changeSort($id, $sort));
    }

    function delete($id = '') {
        $m_file = new \app\xwam\model\FilesModel();
        return json_encode($m_file->deleteFile($id));
    }

    // 2017-3-20
    function category($id = '') {
        if (!$id) {
            $this->dialogClose(false, array('error' => '参数有误!'));
        }
        $mFile = new \app\xwam\model\FilesModel();
        $list = $mFile->where(array('id' => array('in', $id)))->field('id, title, category_id')->order('sort, id desc')->select();
        $this->assign('list', $list);
        return view();
    }

    /**
     * 保存类别
     * @version 1.0 2016-3-20 SoChishun Added.
     */
    public function categorySave() {
        $aid = input('id/a');
        $aCategoryId = input('category_id/a');
        $mFile = new \app\xwam\model\FilesModel();
        $result = $mFile->saveCategory($aid, $aCategoryId);
        if (!$result['status']) {
            $this->dialogClose(false, array('error' => '参数有误!'));
        }
        $this->dialogJump(true);
    }

}
